<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 评论
 *
 * @author      Eden
 * @datetime    2016/12/18 22:20
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Commentlist_Model extends API_Model
{

    const PAGE_SIZE = 20;

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 主程序
     * @param array $params
     */
    public function run($params = array())
    {
        $send['state'] = false;
        do {

            if (!$this->checkParams($params)) {
                $send['error'] = $this->getError();
                break;
            }

            $page = !empty($params['page']) && intval($params['page']) > 0 ? intval($params['page']) : 1;
            $offset = ($page - 1) * self::PAGE_SIZE;

            $rows = $this->getLists($params['goodsId'], $offset, self::PAGE_SIZE);
            $send['state'] = true;
            if (!$rows) {
                $send['data'] = [
                    'total' => 0,
                    'nextPage' => false,
                    'data' => []
                ];
            } else {
                $send['data'] = $rows;
            }
        } while(false);
        $this->output->send($send);
    }

    /**
     * 评论列表
     * @param $goodsId
     * @param int $offset
     * @param int $limit
     * @return mixed
     */
    private function getLists($goodsId, $offset = 0, $limit = 20)
    {

        $where = array('c.goodsId' => $goodsId, 'c.status' => 1);
        $tDb = clone($this->db);
        $tDb->where($where);
        $total = $tDb->count_all_results('base_goods_comments as c');

        if (empty($total)) {
            return false;
        }

        $db = clone($this->db);
        $q = $db->select('c.id as commentId, c.content, c.created, c.anonymous, u.id as userId, u.nickName, p.avatar')
            ->where($where)
            ->limit($limit, $offset)
            ->join('user as u', 'u.id = c.userId')
            ->join('user_profile as p', 'p.userId = c.userId')
            ->from('base_goods_comments as c')
            ->order_by('c.created', 'DESC')
            ->get();

        $row = $q->result_array();

        $returns = [];
        if (!empty($row)) foreach($row as $item){
            $avatar = empty($item['anonymous']) ? thumb($item['avatar'], 100, 100) : '';
            $returns[] = [
                'commentId' => intval($item['commentId']),
                'userId' => intval($item['userId']),
                'nickName' => empty($item['anonymous']) ? value($item, 'nickName') : '匿名',
                'avatar' => $avatar,
                'content' => value($item, 'content'),
                'created' => formateTimeStamp($item['created']),
            ];
        }

        return array(
            'total' => intval($total),
            'nextPage' => ($offset + $limit) < $total,
            'data' => $returns
        );
    }

    /**
     * 检查参数
     * @param array $params
     * @return bool
     */
    private function checkParams(&$params = array())
    {
        if (empty($params['goodsId'])) {
            $this->setError('参数错误');
            return false;
        }
        return true;
    }
}
